package com.genius.adminmanager.group.service.impl;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.group.entity.GroupEntity;
import com.genius.adminmanager.group.service.GroupServiceAdapter;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.role.entity.RoleJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.workflow.element.SQLBean;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;

@Service("groupEditService")
public class GroupEditServiceImpl extends GroupServiceAdapter{

	@Override
	public JSONObject edit(JSONObject json, JSONObject response) {
		
		try {
			System.out.println(json);
			String status = json.containsKey(GroupEntity.status)?"1":"0";
			json.put(GroupEntity.status, status);
			String pId = json.getString("pId");
			String oldpId = json.getString("oldpId");
			String updateField = GroupEntity.name+","+GroupEntity.status;
			//原pId与现pId不相同，认为进行了父级修改
			if(!pId.equals(oldpId)){
				updateField += ","+GroupEntity.pId+","+GroupEntity.path;
				//设置path的值
				String path = getGroupPath(pId, json.getString("groupId"));
				json.put(GroupEntity.path, path);
				path = null;
			}
			String whereField = GroupEntity.groupId;
			String table = "t_group";
			System.out.println(updateField);
			DBTools.update(updateField, whereField, table, json);
			
			LinkedList<SQLBean> beanList = new LinkedList<SQLBean>();
			String updateSql = DBTools.getUpdateAndWhereSql(updateField, table, whereField);
			String[] updateParam = DBTools.getUpdateAndWhereParms(updateField, json, whereField);
			SQLBean updateBean = new SQLBean(updateSql, updateParam);
			
			String oldpath = json.getString("oldpath");
			String path = json.getString("path");
			String updateziSql = "update t_group set path = REPLACE(path,'"+oldpath+"/','"+path+"/') "
					+ " where path LIKE CONCAT('"+oldpath+"/','%')";
			System.out.println(updateziSql);
			SQLBean updateziBean = new SQLBean(updateziSql);
			
			beanList.add(updateBean);
			beanList.add(updateziBean);
			DBTools.executeBatch(beanList);
			
 			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, "保存组织机构成功");
		} catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Error);
		}
		
		return response;
	}

	//获取path
		private String getGroupPath(String pId,String groupId) throws ConnectionException, DBClosedException, SQLException{
			StringBuilder pathSb = new StringBuilder();
			//根目录下直接取groupId
			if("-1".equals(pId)){
				pathSb.append(groupId);
			}else{
				String pathSql = "select path,groupId from t_group where groupId = '"+pId+"'";
				JSONObject pathJson = DBUtils.select(pathSql);
				pathSb.append(pathJson.getString("path")+"/"+groupId);
			}
			return pathSb.toString();
		}

		@Override
		public JSONObject getUser(JSONObject json, JSONObject response) {
			try {
				String userField = UserJson.userId+" value,"+UserJson.name+" title,"+UserJson.phone+","+UserJson.groupId;
				String userTable = "t_user";
				String whereField = UserJson.status;
				String selectUser = DBTools.getSelectAndWhereSql(userField, userTable, whereField)+" and groupId <> '0'";
				
				String[] parms = new String[]{"1"};
				JSONArray userArr = DBUtils.selectArr(selectUser,parms);
				response.put(ResponseJson.data, userArr);
				response.put(ResponseJson.status, ResponseJson.status_Success);
				
			} catch (ConnectionException e) {
				response.put(ResponseJson.status, ResponseJson.status_Failed);
	    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
			} catch (DBClosedException e) {
				response.put(ResponseJson.status, ResponseJson.status_Failed);
	    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
			} catch (SQLException e) {
				response.put(ResponseJson.status, ResponseJson.status_Failed);
				response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
			}		
			return response;
		}

		@Override
		public JSONObject editUser(JSONObject json, JSONObject response) {
			try {
//				System.out.println(json);
				String userField = UserJson.groupId;
				String userTable = "t_user";
				String whereField = UserJson.userId;
				
				DBTools2.updateBatch(userField, whereField, userTable, json.getJSONArray("userArray"));
				
	 			response.put(ResponseJson.status, ResponseJson.status_Success);
				response.put(ResponseJson.msg, "所属人员保存成功");
			} catch (ConnectionException e) {
				response.put(ResponseJson.status, ResponseJson.status_Failed);
	    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
			} catch (DBClosedException e) {
				response.put(ResponseJson.status, ResponseJson.status_Failed);
				response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
			} catch (SQLException e) {
				response.put(ResponseJson.status, ResponseJson.status_Failed);
				response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Error);
			}
			
			return response;
		}
		@Override
		public JSONObject editSupervisor(JSONObject json, JSONObject response) {
			try {
//				System.out.println(json);
				String updateField = GroupEntity.supervisor;
				String userTable = "t_group";
				String whereField = GroupEntity.groupId;
				
//				DBTools2.updateBatch(userField, whereField, userTable, json.getJSONArray("userArray"));
				DBTools2.update(updateField, whereField, userTable, json);
				
	 			response.put(ResponseJson.status, ResponseJson.status_Success);
				response.put(ResponseJson.msg, "主管修改成功");
			} catch (ConnectionException e) {
				response.put(ResponseJson.status, ResponseJson.status_Failed);
	    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
			} catch (DBClosedException e) {
				response.put(ResponseJson.status, ResponseJson.status_Failed);
				response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
			} catch (SQLException e) {
				response.put(ResponseJson.status, ResponseJson.status_Failed);
				response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Error);
			}
			
			return response;
		}
}
